<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>07_猜数字练习</title>
</head>
<body>
<input type="text" placeholder="请输入数字1-100" id="number">
<button onclick="aandb()">按钮</button>
<hr>
<h4>结果：<span></span></h4>
<script>
    let number =parseInt(Math.random()*100)+1;
    let count = 0;
    function aandb(){
        let n = document.getElementById("number").value;
        let spanE = document.querySelector('span');
        // if (!/^\d+$/.test(n)){
        //     spanE.innerHTML = "请输入数字";
        // }
            if(n==''){
                spanE.innerHTML = "请输入数字";
                 return;
            }
             if (isNaN(n)) {
                spanE.innerHTML = "请输入有效的数字";
                return;
            }
            n = parseInt(n);
            if(n<1||n>100){
                spanE.innerHTML = "请输入1-100的数字";
                return;
            }
            count++;
            if(n>number){
                spanE.innerHTML = "太大了";
            }else if(n<number){
                spanE.innerHTML = "太小了";
            }else{
                spanE.innerHTML = "恭喜你猜对了"+count+"次";
                count=0;
        }
    }
</script>

</body>
</html>